package com.cm.sys.system.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.utils.PageList;
import com.cm.sys.system.dao.AlarmMessageMapper;
import com.cm.sys.system.dao.UserMessageMapper;
import com.cm.sys.system.entity.AlarmMessage;
import com.cm.sys.system.entity.UserMessage;
import com.cm.sys.system.service.MessageService;

/***
 * 站内信消息服务层
 * 
 * @author yxq18
 * @date 2020/01/06
 */
@Service
public class MessageServiceImpl implements MessageService {

    private static final Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private AlarmMessageMapper alarmMessageMapper;

    @Autowired
    private PageList pageList;

    @Override
    public Map<String, Object> selectMessageList(Integer pageIndex, Integer pageSize, Integer userId) {

        // logger.info("用户列表查询的入参==================【{}】===============", args);

        pageList.initPage(pageIndex, pageSize);

        List<Map<String, Object>> messageList = userMessageMapper.getMessageList(userId);

        Map<String, Object> map = pageList.getData(messageList, Map.class);
        logger.info("站内信列表查询的入参==================【{}】===============", new JSONObject(map));
        return map;

    }

    @Override
    public Integer getUnReadMessageCount(Integer userId) {
        Integer count = alarmMessageMapper.getUnreadCount(userId);
        return count;

    }

    @Override
    public AlarmMessage getMessageInfo(Integer messageId) {
        AlarmMessage alarmMessage = alarmMessageMapper.selectByPrimaryKey(messageId);

        return alarmMessage;
    }

    @Override
    public UserMessage saveMessage(Integer messageId, Integer userId, Integer messageState) {

        UserMessage userMessage = new UserMessage();
        userMessage.setMessageId(messageId);
        userMessage.setUserId(userId);
        userMessage.setMessageState(1);
        userMessageMapper.insert(userMessage);
        return userMessage;
    }

    @Override
    public UserMessage getMessage(Integer messageId, Integer userId) {
        UserMessage um = userMessageMapper.getMessageByuserId(messageId, userId);
        return um;
    }

}
